package org.test;

import org.apache.commons.lang3.StringUtils;

/**
 * 度分秒的形式
 *
 * @author <a href="mailto:panpan.chen@vtradex.com">陈盼盼</a>
 * @version v1.0
 * @since 2024/11/21 15:05
 **/
public class DFMUtil {
    /**
     * 纬度 转换成 度分秒的形式
     *
     * @param latitude
     * @return
     */
    public static DFMDto latitudeToDFM(String latitude) {
        String a = latitude;
        String prefix = "";
        Integer[] res = new Integer[3];
        for (int i = 0; i < 3; i++) {
            prefix = StringUtils.substringBefore(a, ".");
            res[i] = Integer.parseInt(prefix);
            a = "0." + StringUtils.substringAfter(a, ".");
            a = Double.parseDouble(a) * 60 + "";
        }
        return new DFMDto(res[0], res[1], res[2]);
    }

    /**
     * 经度 转换成 度分秒的形式
     *
     * @param longitude
     * @return
     */
    public static DFMDto longitudeToDFM(String longitude) {
        String a = longitude;
        String prefix = "";
        Integer[] res = new Integer[3];
        for (int i = 0; i < 3; i++) {
            prefix = StringUtils.substringBefore(a, ".");
            res[i] = Integer.parseInt(prefix);
            a = "0." + StringUtils.substringAfter(a, ".");
            a = Double.parseDouble(a) * 60 + "";
        }
        return new DFMDto(res[0], res[1], res[2]);
    }

    public static String toDFMString(Point point) {
        DFMDto dfmDto2 = longitudeToDFM(point.getLongitude().toString());
        DFMDto dfmDto = latitudeToDFM(point.getLatitude().toString());
        return dfmDto.asString() + "," + dfmDto2.asString();
    }

    public static void main(String[] args) {
        String dfmString = DFMUtil.toDFMString(Point.parse("117.131179,39.063103", ""));
        System.out.println(dfmString);
    }
}
